cmake_minimum_required(VERSION 3.10)
project(travoddsgen_test_c)

set(idl_file ${CMAKE_CURRENT_SOURCE_DIR}/../test.idl)
get_filename_component(idl_file_abs ${idl_file} ABSOLUTE)

set(output_dir ${CMAKE_CURRENT_BINARY_DIR}/output)
get_filename_component(output_dir_abs ${output_dir} ABSOLUTE)
file(MAKE_DIRECTORY ${output_dir_abs})

set(output_files ${output_dir_abs}/test.cpp ${output_dir_abs}/test.h ${output_dir_abs}/testTypeSupport.h ${output_dir_abs}/testTypeSupport.cpp)

add_custom_command(
    OUTPUT ${output_files}
    COMMAND travoddsgen --input ${idl_file_abs} --output_dir ${output_dir_abs} --language c
    DEPENDS travoddsgen ${idl_file_abs}
    COMMENT "Generating C dds file using travoddsgen"
)

add_custom_target(generate_c ALL DEPENDS ${output_files})

add_library(testTypeCLib STATIC ${output_files})
target_include_directories(testTypeCLib PUBLIC ${output_dir_abs})
target_include_directories(testTypeCLib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_include_directories(testTypeCLib PRIVATE ${travoddscz_INCLUDE_DIRS})
target_include_directories(testTypeCLib PRIVATE ${travoddsz_INCLUDE_DIRS})
add_dependencies(testTypeCLib generate_c)

add_executable(pub_c pub_c.c)
target_include_directories(pub_c PRIVATE ${output_dir_abs})
target_include_directories(pub_c PRIVATE ${travoddscz_INCLUDE_DIRS})
target_link_libraries(pub_c PRIVATE testTypeCLib)
target_link_libraries(pub_c PRIVATE ${travoddscz_LIBRARIES} ${travoddsz_LIBRARIES})

add_executable(sub_c sub_c.c)
target_include_directories(sub_c PRIVATE ${output_dir_abs})
target_include_directories(sub_c PRIVATE ${travoddscz_INCLUDE_DIRS})
target_link_libraries(sub_c PRIVATE testTypeCLib)
target_link_libraries(sub_c PRIVATE ${travoddscz_LIBRARIES} ${travoddsz_LIBRARIES})
